
*use processed\RepresentativeRPI,replace

use processed/cpisave/cpi_master.dta,clear

keep I	datayear weight	quant b* 

bysort datayear quant: egen double Itot = total(I*weight)
bysort datayear quant: egen double wtot = total(weight)
forvalues x = 1/85	 {
gen e`x' = b`x'*I*weight
bysort datayear quant: egen double e`x'tot = total(e`x')
}
drop b* I

forvalues x = 1/85	 {
gen b`x' = e`x'tot/Itot
}
 
drop Itot	wtot e* weight

duplicates drop datayear quant,force

merge m:1 datayear using processed/cpisave/dlogcpi_master.dta
drop _merge 

egen group_id = group(datayear quant)
drop if quant == .


reshape long b dlogp, i(group_id) j(goods_id)

egen group_id2 = group(goods_id quant)

	xtset group_id2  datayear
	gen  dlogTOR_tmp = (1/2)*(l.b+b)*dlogp 
	bysort datayear quant: egen dlogTOR = total(dlogTOR_tmp)
	duplicates drop datayear quant,force


keep datayear	quant dlogTOR

sort quant datayear

reshape wide dlogTOR, i(datayear)  j(quant)


outsheet using DataforMatlab\torn_CPIdec.csv, comma replace nonames

	
	
